package com.greyu.ysj.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.greyu.ysj.config.ResultStatus;
import com.greyu.ysj.entity.CbtBallot;
import com.greyu.ysj.entity.CbtCoininfo;
import com.greyu.ysj.entity.CbtCoininfoExample;
import com.greyu.ysj.entity.CbtInvest;
import com.greyu.ysj.entity.CbtRecharge;
import com.greyu.ysj.mapper.CbtCoininfoMapper;
import com.greyu.ysj.model.ResultModel;
import com.greyu.ysj.service.CbtCoinInfoService;

@Service
@Transactional
public class CbtCoinInfoServiceImpl implements CbtCoinInfoService {
	@Autowired
	private CbtCoininfoMapper cbtCoininfoMapper;

	
	@Override
	public CbtCoininfo selectCoinInfoBycoinId(Integer coinId) {
		
		return this.cbtCoininfoMapper.selectByPrimaryKey(coinId);
	}


	@Override
	public ResultModel save(CbtCoininfo cbtCoininfo) {
		try {	
			if ( cbtCoininfo.getCoinChineseName()==null
					||cbtCoininfo.getCoinSymbol()==null
	        		) {
	            return ResultModel.error(ResultStatus.DATA_NOT_NULL);
	        }
			//insert
	        this.cbtCoininfoMapper.insert(cbtCoininfo);
		} catch (Exception e) {
			return ResultModel.error(ResultStatus.INSERT_ERROR);
		}
	        return ResultModel.ok(cbtCoininfo);
		}


	@Override
	public CbtCoininfo selectCoinInfoBycoinSymbol(String coinSymbol) {
		CbtCoininfoExample cbtCoininfoExample = new CbtCoininfoExample();
        CbtCoininfoExample.Criteria criteria = cbtCoininfoExample.createCriteria();
        criteria.andCoinSymbolEqualTo(coinSymbol);
        List<CbtCoininfo> coinList = this.cbtCoininfoMapper.selectByExample(cbtCoininfoExample);

        CbtCoininfo cbtCoininfo;
        try {
            cbtCoininfo = coinList.get(0);
        } catch (Exception e) {
            return null;
        }
		return cbtCoininfo;
	}

	@Override
	public List<CbtCoininfo> selectCoinList(Integer page, Integer rows,Integer userId) {
		if (null != page && null != rows) {
            PageHelper.startPage(page, rows);
        }
		List<CbtCoininfo> list=cbtCoininfoMapper.selectCoinListByUserId(userId);
		if(list==null||list.size()<1) {
			return null;
		}
		/*for(CbtCoininfo coin:list) {
			CbtRecharge cbtRecharge=coin.getCbtRecharge();
			System.out.println("%%%%%%%%%%%%%%%%"+coin);
			double amount=cbtRecharge.getAmount();
			System.out.println("%$%$^&&*&*)())&*"+amount);
		}*/
		
		return list;
	}


	@Override
	public int selectCoinCount() {
		int total=cbtCoininfoMapper.selectCoinCount();
		return total;
	}


	@Override
	public List<CbtCoininfo> selectAllCoins() {
        CbtCoininfoExample cbtCoininfoExample = new CbtCoininfoExample(); 
        List<CbtCoininfo> coins = this.cbtCoininfoMapper.selectByExample(cbtCoininfoExample);
        return coins;
	}


	@Override
	public List<CbtCoininfo> getInvestRecordByuserId(Integer page, Integer rows, Integer userId) {
		if (null != page && null != rows) {
            PageHelper.startPage(page, rows);
        }
		List<CbtCoininfo> investRecord=cbtCoininfoMapper.selectInvestRecordByuserId(userId);
		
		return investRecord;
	}


	@Override
	public List<CbtCoininfo> getRechargeRecordByuserId(Integer page, Integer rows, Integer userId,Integer coinId) {
		if (null != page && null != rows) {
            PageHelper.startPage(page, rows);
        }
		List<CbtCoininfo> rechargeRecord=cbtCoininfoMapper.selectRechargeRecordByuserId(userId, coinId);
		
		return rechargeRecord;
	}


	@Override
	public String selectCoinLocationBycoinId(Integer coinId) {
		
		return this.cbtCoininfoMapper.selectCoinLocationBycoinId(coinId);
	}

}

